#!/usr/bin/perl

use dba;
use DBI;
use HTTP::Date qw(time2str str2time);
use strict;
#no utf;

#my $path='http://mine/down/WU/Current/VSCANDAT1000/DAT/0000/';#'ftp://cdit/McAfee/inc/';
my $path='http://mine/down/upd/inc/';#'ftp://cdit/McAfee/inc/';
my $cmd=not (exists $ENV{'SERVER_SOFTWARE'} || $ARGV[0] eq 'http');
my $andWhere='';
$andWhere=$ARGV[0],$andWhere=~s/\D//g,$andWhere=" and ver<=".$andWhere if $ARGV[0]>0;

my @known=map"`$_`",qw/McUpd id ver name code size/;
#			($id,$ver,$_,$name,$cod,$sz)
my ($id,$ver,$name,$cod,$sz,$ts);
my $table=shift(@known);
my $pv=join ',',@known;
my $pq=join ',',map'?',@known;
#my $upd=$dba::h->prepare(qq/INSERT ignore $table ($pv) VALUES ($pq)/);
my $s=$dba::h->prepare(qq/select $pv from $table where ver>5300 $andWhere/);
=head
    my %m=%{$h1{'Multiple Patch Table'}};
    my %s=%{$h1{'File Size'}};
    my %u=%{$h1{'Incremental Resolver'}};
    my %c=%{$h1{'Patch Validation'}};
    
    foreach (sort keys %m)
    {
	my ($ver,$id)=($_,$m{$_});
	$_=$u{$id};
	my $name=$_;
	$name=$1 if m{/([^/]*)$};
	my ($cod,$sz)=($c{$name},$s{$name});	
#	print $id,$ver,$_,$name,$cod,$sz;
	$upd->execute($id,$ver,$_,$name,$cod,$sz);
#	print "Key: '$_'='$t{$_}'\n"
    }
=cut
my ($mpt,$fs,$pv,$inc);

#$h1{'Multiple Patch Table'}=[];
#$h1{'File Size'}=[];
#$h1{'Incremental Resolver'}=[];
#{$h1{'Patch Validation'}=[];

$s->execute()||die $DBI::errstr;
my ($v,$i,$n);
$path='' if $cmd;
while (($id,$ver,$name,$cod,$sz)=$s->fetchrow_array())
{   
    $mpt.="$ver=$id\n";
    $inc.="$id=$path$name\n";
    $pv .="$name=$cod\n";
    $fs .="$name=$sz\n";
    ($v,$i,$n)=($ver,$id,$name);
}
$v++;

$ts=time2str((stat"inc/$n")[9]||0);

$_=<<EOF;
[Contents]
LatestIncremental=$i
Created=$ts
CurrentVersion=$v

[Multiple Patch Table]
$mpt
[Incremental Resolver]
$inc
[Single Patch Table]

[Patch Validation]
$pv
[File Size]
$fs
EOF
s{\r?\n}{\r\n}g;
print "Content-Type: text/plain\r\n\r\n" unless $cmd;
print;
